<?php
class PaymentBilling extends PaymentData{
	
	public $customerId;
	public $firstName;
	public $lastName;
	public $address1;
	public $address2;
	public $city;
	public $state;
	public $country;
	public $zip;
	public $email;
	public $phone;	
	public $billingId;
	
	public function __construct(){
		$this->setName('billingInfo');
	}
	
	public function setBillingInfo($billingInfo){
		foreach($billingInfo as $key => $val){
			if (property_exists($this,$key)){
				$this->$key = $val;
			}
		}
		
	}
	
	public function toArray(){
		return array(
			'billingId'		=> 	$this->billingId,
			'customerId'	=>	$this->customerId,
			'firstName'		=>	$this->firstName,
	 		'lastName'		=>	$this->lastName,
			'address1'		=>	$this->address1,
			'address2'		=>	$this->address2,
			'city'			=>	$this->city,
			'state'			=>	$this->state,
			'country'		=>	$this->country,
			'zip'			=>	$this->zip,
			'email'			=>	$this->email,			
			'phone'			=>	$this->phone,
		);
	}
	
	public function saveInfo(){	
		if(!empty($this->billingId)){
			$params = array(
// 				'id' => $this->billingId,
				'customer_id'			=>	$this->customerId,
				'billing_first_name'	=>	$this->firstName,
				'billing_last_name'		=>	$this->lastName,
				'billing_address1'		=>	$this->address1,
				'billing_address2'		=>	$this->address2,
				'billing_city'			=>	$this->city,
				'billing_state'			=>	$this->state,
				'billing_country'		=>	$this->country,
				'billing_zip'			=>	$this->zip,
				'billing_email'			=>	$this->email,
				'billing_phone'			=>	$this->phone,
			);
			return CustomerOrderBillingInfo::model()->updateAll($params,'customer_id ='.$this->customerId);
		}else{
			$billingInfo = new CustomerOrderBillingInfo();		
			$billingInfo->customer_id 			=	$this->customerId;
			$billingInfo->billing_first_name	=	$this->firstName;
			$billingInfo->billing_last_name		=	$this->lastName;
			$billingInfo->billing_address1		=	$this->address1;
			$billingInfo->billing_address2		=	$this->address2;
			$billingInfo->billing_city			=	$this->city;
			$billingInfo->billing_country		=	$this->country;
			$billingInfo->billing_state			=	$this->state;
			$billingInfo->billing_zip			=	$this->zip;
			$billingInfo->billing_phone			=	$this->phone;
			$billingInfo->billing_email			=	$this->email;
			$billingInfo->created				=	time();
	
			$billingInfo->save(false);
			return $billingInfo->id;
		}
	}
	
	public function saveBillingInfo($id,$customerId,$firstName,$lastName,$address1,$address2,$city,$country,$state,$zip,$phone,$email){
		$billingInfo = new CustomerOrderBillingInfo();
		$db = Yii::app()->db->beginTransaction();
		try {
			if(!empty($id)){
				$params = array(
// 						'id' 					=>  $id,
						'customer_id'			=>	$customerId,
						'billing_first_name'	=>	$firstName,
						'billing_last_name'		=>	$lastName,
						'billing_address1'		=>	$address1,
						'billing_address2'		=>	$address2,
						'billing_city'			=>	$city,
						'billing_state'			=>	$country,
						'billing_country'		=>	$state,
						'billing_zip'			=>	$zip,
						'billing_email'			=>	$email,
						'billing_phone'			=>	$phone,
				);
				return CustomerOrderBillingInfo::model()->updateAll($params,'customer_id ='.$this->customerId);
			}else{
				$billingInfo->customer_id=$customerId;
				$billingInfo->billing_first_name=$firstName;
				$billingInfo->billing_last_name=$lastName;
				$billingInfo->billing_address1=$address1;
				$billingInfo->billing_address2=$address2;
				$billingInfo->billing_city=$city;
				$billingInfo->billing_country=$country;
				$billingInfo->billing_state=$state;
				$billingInfo->billing_zip=$zip;
				$billingInfo->billing_phone=$phone;
				$billingInfo->email=$email;
				$billingInfo->billing_email=time();
				$billingInfo->save(false);
			}
			$db->commit();
		} catch (Exception $ex){
			$db->rollback();
		}
		return $billingInfo->id;
	}
	
	public function vaild(){
// 		if (empty($this->id)){
// 			$this->setErrorMessage('customerId','Customer Id NO');
// 		}
		return $this->vaild;
	}
}